package com.test.profile.controller;

import com.test.profile.pojo.User;

import com.test.profile.service.UserService;
import com.test.profile.service.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@WebServlet("/login")
public class UserController extends BaseServlet{
    public  String login(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(username == null || password == null){
            return  "redirct："+request.getContextPath()+"/admin/login.jsp";
        }
        //调用service 校验用户名和密码
        UserService adminService = new UserServiceImpl();
        User user = adminService.login(username, password);
        //如果非空将得到的用户放进session（一直携带）来传给前端页面
        if(user!=null){
            request.getSession().setAttribute("admin",user);
            return "redirect:"+request.getContextPath()+"/admin/admin.jsp";
        }else {
            return "redirect:"+request.getContextPath()+"/admin/admin.jsp";
        }
    }
    public void register(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        UserServiceImpl userService = new UserServiceImpl();
        int count = userService.addUser(user);
        if(count>0){
            request.setAttribute("msg","注册成功");
        }else{
            request.setAttribute("msg","注册失败请换个用户名");
        }


    }
}
